//
//  SEPrescriptionDiagnosisPickViewModel.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/5/11.
//

import UIKit

class SEPrescriptionDiagnosisPickViewModel: SEBaseViewModel {
    var diagnosisPickList = [SEPrescriptionDiagnosisPick]()
    var diagnosisPickModel = SEPrescriptionDiagnosisPick()
    
    convenience init(diagnosisPickModel: SEPrescriptionDiagnosisPick) {
        self.init()
        self.diagnosisPickModel = diagnosisPickModel
    }
    
    var numberOfRows: Int { diagnosisPickList.count }
    
    func viewModel(at indexPath: IndexPath) -> SEPrescriptionDiagnosisPickViewModel {
        let viewModel = SEPrescriptionDiagnosisPickViewModel(diagnosisPickModel: diagnosisPickList[indexPath.row])
        viewModel.type = type
        return viewModel
    }
    
    var cellHeight: CGFloat { return 70 }
    
    var content: String = ""
    
    var type: SEPrescriptionDiagnosisType = .diagnose
    
    var value: String? {
        type != .diagnose ? diagnosisPickModel.value : diagnosisPickModel.name
    }
}

extension SEPrescriptionDiagnosisPickViewModel {
    /// 获取前缀、后缀列表
    func getListOfDiagnosisPrefixAndSuffix(mark: String) {
        SEHttpManager.shared.request(SEPrescriptionAPI.listOfDiagnosisPrefixAndSuffix(mark: mark), model: [SEPrescriptionDiagnosisPick].self) { (models, httpStatus) in
            switch httpStatus {
            case .success:
                self.diagnosisPickList = models ?? self.diagnosisPickList
            case .auth(let message), .failure(let message):
                SEHUD.showError(message: message)
            }
            self.updateData?(httpStatus)
        }
    }
    
    /// 获取诊断列表
    func getListOfDiagnosisResult(_ more: Bool) {
        page = more ? page + 1 : 1
        SEHttpManager.shared.request(SEPrescriptionAPI.listOfDiagnosisResult(content: content, pageNum: page, pageSize: pageSize), model: SEPrescriptionDiagnosisPickList.self) { (model, httpStatus) in
            switch httpStatus {
            case .success:
                if !more {
                    self.diagnosisPickList = model?.list ?? self.diagnosisPickList
                } else {
                    self.diagnosisPickList.append(contentsOf: model?.list ?? [])
                }
                self.hasMore = model?.totalPage ?? 0 > self.page
            case .auth(let message), .failure(let message):
                SEHUD.showError(message: message, position: .center)
            }
            self.updateData?(httpStatus)
        }
    }
}
